﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class UserControl_ControlQLTTKenhBanHang : System.Web.UI.UserControl
{
   
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error ="";
   
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["flag"] == null)
            {
                //ViewState["flag"] = false;
                ClearAdd();
            }
        }
    }

    void ClearAdd()
    {
        TabContainer1.ActiveTabIndex = 0;
        clearDetail();
        ViewState["flag"] = true;
        txtChannelName.Focus();

        btnSave.Text = "Lưu";
        lblMsg.Text = "";
        lblMsg.Visible = false;
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }

    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        ClearAdd();
    }

    protected void grvChannelDr_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == DRMConstants.COMMAND_NAME_ROW_COMMAND)
        {
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;

            Label lblMaSo = (Label)grv.FindControl("lblMaSo");
            Label lblChannelName = (Label)grv.FindControl("lblChannelName");            

            txtMaSo.Text = lblMaSo.Text;
            txtChannelName.Text = lblChannelName.Text;
            
            ViewState["flag"] = false;
            btnSave.Text = "Cập nhật";
            lblMsg.Text = "";
            lblMsg.Visible = false;

            grvChannelDr.DataSource = null;
            grvChannelDr.DataBind();

            TabContainer1.ActiveTabIndex = 0;
        }
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        LoadGrid();
    }

    protected void grvChannelDr_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvChannelDr.PageIndex = e.NewPageIndex;
        LoadGrid();
    }
   
    private void LoadGrid()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "TTr_ChannelDr_Search", CommandType.StoredProcedure,
                new SqlParameter("@Cont", txtSearch.Text));


            if (db.Rows.Count == 0)
            {
                ShowMessageBox("Không tìm thấy kết quả");
                grvChannelDr.DataSource = null;
            }
            else
            {
                grvChannelDr.DataSource = db;
            }
            grvChannelDr.DataBind();
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }

    private void clearDetail()
    {
        txtChannelName.Text = "";
        txtMaSo.Text = "New";
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if ((bool)ViewState["flag"])
            {
                //insert                 

                if (dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "TTr_ChannelDr_Insert", ref error,
                    new SqlParameter("@ChannelName", txtChannelName.Text)))
                {   
                    ClearAdd();
                    lblMsg.Text = "Thêm mới kênh bán hàng thành công";
                    lblMsg.Visible = true;
                }                
                else
                {
                    lblMsg.Text = "Hệ thống đang bảo trì";
                    lblMsg.Visible = true;
                }
                
            }
            else
            {
                //update
                
                if (dt.ExecuteSQl_bool(str, CommandType.StoredProcedure,"TTr_ChannelDr_Update", ref error,
                    new SqlParameter("@ChannelID", txtMaSo.Text),
                    new SqlParameter("@ChannelName", txtChannelName.Text)))
                {
                    lblMsg.Text = "Cập nhật kênh bán hàng thành công";
                    lblMsg.Visible = true;
                }
                else
                {
                    lblMsg.Text = "Hệ thống đang bảo trì";
                    lblMsg.Visible = true;
                }
            }
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
  
}